<template>
  <div class="kanban-container">
    <div class="kanban-board">
      <div 
        v-for="status in statusList" 
        :key="status.value" 
        class="kanban-column"
        :class="'column-' + status.value"
      >
        <div class="kanban-column-header">
          <div class="column-title">
            <span class="status-dot" :class="'status-' + status.value"></span>
            {{ status.label }}
          </div>
          <div class="column-count">{{ getProjectsByStatus(status.value).length }}</div>
        </div>
        <div 
          class="kanban-cards"
          @dragover.prevent
          @drop="handleDrop($event, status.value)"
        >
          <div 
            v-for="project in getProjectsByStatus(status.value)" 
            :key="project.id" 
            class="kanban-card"
            draggable="true"
            @dragstart="handleDragStart($event, project)"
            @dragend="handleDragEnd"
            @click="$emit('project-click', project)"
          >
            <div class="card-header">
              <h3 class="card-title">{{ project.name }}</h3>
              <el-progress 
                :percentage="project.progress" 
                :status="project.progress === 100 ? 'success' : ''"
                :stroke-width="5"
              ></el-progress>
            </div>
            <div class="card-content">
              <p class="card-description">{{ truncateText(project.description, 100) }}</p>
            </div>
            <div class="card-footer">
              <div class="card-dates">
                <span class="date-label">开始: </span>
                <span class="date-value">{{ formatDate(project.startDate) }}</span>
                <span class="date-separator">-</span>
                <span class="date-label">截止: </span>
                <span class="date-value">{{ formatDate(project.endDate) }}</span>
              </div>
              <div class="card-manager">
                <span class="manager-label">负责人: </span>
                <span class="manager-value">{{ project.manager }}</span>
              </div>
              <div class="card-members" v-if="project.members && project.members.length">
                <div class="members-avatars">
                  <el-tooltip 
                    v-for="(member, index) in project.members.slice(0, 3)" 
                    :key="index" 
                    :content="member" 
                    placement="top"
                  >
                    <div class="member-avatar">{{ member.charAt(0) }}</div>
                  </el-tooltip>
                  <div class="member-more" v-if="project.members.length > 3">
                    +{{ project.members.length - 3 }}
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div 
            class="empty-placeholder" 
            v-if="getProjectsByStatus(status.value).length === 0"
            @dragover.prevent
            @drop="handleDrop($event, status.value)"
          >
            拖动项目到此处
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  projects: {
    type: Array,
    required: true
  }
})

const emit = defineEmits(['project-click', 'status-change'])

// 状态列表
const statusList = [
  { label: '待启动', value: 'pending' },
  { label: '进行中', value: 'in-progress' },
  { label: '已完成', value: 'completed' },
  { label: '已暂停', value: 'paused' }
]

// 当前拖拽的项目
const draggedProject = ref(null)

// 根据状态获取项目
const getProjectsByStatus = (status) => {
  return props.projects.filter(project => project.status === status)
}

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 截断文本
const truncateText = (text, maxLength) => {
  if (!text) return ''
  return text.length > maxLength ? text.substring(0, maxLength) + '...' : text
}

// 拖拽开始
const handleDragStart = (event, project) => {
  draggedProject.value = project
  event.dataTransfer.effectAllowed = 'move'
  // 设置拖拽时的半透明效果
  event.target.classList.add('dragging')
}

// 拖拽结束
const handleDragEnd = (event) => {
  event.target.classList.remove('dragging')
}

// 放置处理
const handleDrop = (event, newStatus) => {
  event.preventDefault()
  
  if (!draggedProject.value) return
  
  // 如果状态没有变化，不做处理
  if (draggedProject.value.status === newStatus) return
  
  // 发送状态变更事件
  emit('status-change', {
    projectId: draggedProject.value.id,
    oldStatus: draggedProject.value.status,
    newStatus: newStatus
  })
  
  // 更新项目进度
  let newProgress = draggedProject.value.progress
  if (newStatus === 'completed') {
    newProgress = 100
  } else if (newStatus === 'pending') {
    newProgress = 0
  } else if (newStatus === 'in-progress' && draggedProject.value.status === 'pending') {
    newProgress = 30
  }
  
  // 提示状态变更
  ElMessage.success(`项目"${draggedProject.value.name}"已移动到"${statusList.find(s => s.value === newStatus).label}"`)
  
  // 清除拖拽项目
  draggedProject.value = null
}
</script>

<style scoped>
.kanban-container {
  width: 100%;
  overflow-x: auto;
  padding: 10px 0;
}

.kanban-board {
  display: flex;
  min-height: 600px;
  gap: 20px;
}

.kanban-column {
  flex: 1;
  min-width: 280px;
  background-color: #f5f7fa;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
}

.kanban-column-header {
  padding: 12px 16px;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.column-title {
  font-weight: bold;
  font-size: 16px;
  display: flex;
  align-items: center;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 8px;
}

.status-dot.status-pending {
  background-color: #909399;
}

.status-dot.status-in-progress {
  background-color: #409EFF;
}

.status-dot.status-completed {
  background-color: #67C23A;
}

.status-dot.status-paused {
  background-color: #E6A23C;
}

.column-count {
  background-color: #ecf5ff;
  color: #409EFF;
  border-radius: 10px;
  padding: 2px 8px;
  font-size: 12px;
}

.kanban-cards {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 200px; /* 确保空列也有足够的高度用于拖放 */
}

.kanban-card {
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  padding: 12px;
  cursor: pointer;
  transition: all 0.3s;
}

.kanban-card:hover {
  box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.kanban-card.dragging {
  opacity: 0.5;
  transform: scale(0.95);
}

.card-header {
  margin-bottom: 10px;
}

.card-title {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: #303133;
}

.card-content {
  margin-bottom: 10px;
}

.card-description {
  margin: 0;
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}

.card-footer {
  font-size: 12px;
  color: #909399;
}

.card-dates {
  margin-bottom: 5px;
}

.date-label, .manager-label {
  color: #909399;
}

.date-value, .manager-value {
  color: #606266;
}

.date-separator {
  margin: 0 5px;
}

.card-manager {
  margin-bottom: 8px;
}

.members-avatars {
  display: flex;
  margin-top: 8px;
}

.member-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background-color: #409EFF;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
  margin-right: -6px;
  border: 2px solid white;
}

.member-more {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background-color: #f0f2f5;
  color: #909399;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
  margin-left: 4px;
  border: 2px solid white;
}

.empty-placeholder {
  padding: 20px;
  text-align: center;
  color: #909399;
  font-size: 14px;
  background-color: rgba(255, 255, 255, 0.5);
  border-radius: 4px;
  border: 1px dashed #dcdfe6;
  min-height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.kanban-column:hover .empty-placeholder {
  background-color: rgba(64, 158, 255, 0.1);
  border-color: #409EFF;
}
</style> 